import logging

from digvcs.lib.base import BaseController
from digvcs.lib.base import render
from digvcs.model import meta
from digvcs.model.svnrepo import Repository
from digvcs.model.svnrepo import Revision
from pylons import request
from pylons import tmpl_context as c
from pylons import url
from pylons.controllers.util import redirect_to

log = logging.getLogger(__name__)

class LogController(BaseController):
    """Provides with plain repository log browsing actions"""

    def __init__(self):
        """Documentation"""
        BaseController.__init__(self)
        self.type = None

    def __before__(self):
        if (request.params.has_key('type')):
            print request.params['type']
            self.type = request.params['type']
        else:
            redirect_to(url('index', anchor=request.path))

    def index(self):
        """Displays a list of managed repositories"""
        c.repositories = meta.Session.query(Repository).order_by('url').all()        
        return render('log/html/index.html')

    def repository(self, repo):
        """Displays repository with a list of revisions"""
        c.repository = meta.Session.query(Repository).filter_by(id=repo).first()
        c.revisions = c.repository.revisions
        return render('log/html/repository.html')

        
    def revision(self, repo, rev):
        """Displays revision with a list of changes"""
        c.repository = meta.Session.query(Repository).filter_by(id=repo).first()
        c.revision = meta.Session.query(Revision) \
            .filter(Revision.repository == c.repository) \
            .filter(Revision.rev_num == rev) \
            .first()
        c.changes = c.revision.changes
        return render('log/html/revision.html')
        